<?php 
include_once("db_backup/global_conf.php");
include_once("db_backup/exportData.php");
include_once("db_backup/main.php");


if(@$backup)
{
	$mesg=$M_backupCreated;
}

 ?>
<?php require_once('include/header.php'); ?>
      <div id="middle_container_for_innerpage">
      <div id="innerpage_top_image"><img src="images/images/innerpage_top_corner_820x5.jpg"  height="5" /></div> 
      <div id="middle_inner_container">
      <?php require_once('include/left_side_menu.php'); ?>
	          
        <div id="inner_page_middle_data"><table width="100%" border="0" cellspacing="0" cellpadding="0" align="center">
	<tr>
		<td height="10"></td>
	</tr>
	<tr>
		<td class="admin-text" align="center">DB Utility</td>
	</tr>
	<tr>
		<td height="10"></td>
	</tr>
	<?if($mesg){?>
	<tr>
		<td height="10" align="center" class="mesg"><?=$mesg?></td>
	</tr>
	<?}?>
	<tr><td>
				   <?php
						// set the timelimit
						//set_time_limit($CONF['time_limit']);
						set_time_limit(120);

						if(isset($argv[1])) {
							$mode="shell";
							$HTTP_POST_VARS['db']=$argv[1];
							$HTTP_POST_VARS['data']="on";
							$HTTP_POST_VARS['tables']="on";

						} else {
							$mode="web";
						}
						$phpMyBackup=new phpMyBackup;
						if($mode=="web") {
							//$phpMyBackup->printHeader();
						}

						if(isset($HTTP_POST_VARS['db'])) {
							delOldBackups();
							$exportLib=new dropTable;
							$phpMyBackup->sqlConnect($CONF['sql_host'],$CONF['sql_usr'],$CONF['sql_pass']);
							if($mode=="web") {
								if($HTTP_POST_VARS['db']=="multiple") {
									$db_list=explode(",", $HTTP_POST_VARS['db_list']);
								} else {
									$db_list[]=$HTTP_POST_VARS['db'];
								}
							} else {
								 $db_list=explode(",",$HTTP_POST_VARS['db']);
							}
							foreach($db_list as $export_db) {
							if(!mysql_select_db($export_db,$con)) {
								echo"ERROR: Could not select db $export_db<br><br>\n\n";
							} else {
							$exportLib->dbName=$export_db;
							$fileData=$exportLib->structure();
							$today = getdate();
							$month = $today[mon];
							if($month < 10)
							{
								$month = "0".$month;
							}
							$day = $today[mday];
							if($day < 10)
							{
								$day = "0".$day;
							}
							$year = $today[year];
							$hours = $today[hours];
							if($hours < 10)
							{
								$hours = "0".$hours;
							}
							$min = $today[minutes];
							if($min < 10)
							{
								$min = "0".$min;
							}
							$time=$day.$month.$year.$hours.$min;
							//$time = date("mdYHi",$time);
							// see if zlib is installed
							if (function_exists('gzopen')) {
								// open file for writing with maximum compression
								$zp = gzopen("./export/".$exportLib->dbName."_".$time.".sql.gz", "w9");
								gzwrite($zp, $fileData);
								gzclose($zp);
								$BackupFileName=$exportLib->dbName."_".$time.".sql.gz";
								$BackupIsGziped=1;
							} else {
								// write file old fasion way
								$fp=fopen("./export/".$exportLib->dbName."_".$time.".sql", "w");
								fwrite($fp,$exportLib->structure());
								fclose($fp);
								$BackupIsGziped=0;
								$BackupFileName=$exportLib->dbName."_".$time.".sql";
							}

							$backup_size=strlen($fileData);
							if(isset($HTTP_POST_VARS['data']) && $HTTP_POST_VARS['data']=="on") {
								$store_data="yes";
							} else {
								$store_data="no";
							}

							if(isset($HTTP_POST_VARS['tables']) && $HTTP_POST_VARS['tables']=="on") {
								$store_tables="yes";
							} else {
								$store_tables="no";
							}

							$fp=fopen("./export/".$exportLib->dbName."_".$time.".info", "w");
							$comments=str_replace("|","l",$HTTP_POST_VARS['comments']);
							fwrite($fp,"$time|$export_db|$backup_size|$store_tables|$store_data|$BackupIsGziped|$comments");
							fclose($fp);

							chmod("./export/".$BackupFileName,0777);
							chmod("./export/".$exportLib->dbName."_".$time.".info",0777);


							if($CONF['ftp_use']==1 && function_exists('ftp_connect')) {
								$ftp=ftp_store($BackupFileName);
								$ftp .= ftp_store($exportLib->dbName."_".$time.".info");
							}
							if($mode=="web") {
								echo"<table cellspacing='0' cellpadding='0' border=0 align='center' width='300' class=\"table-rows\">";
								echo"<tr><td align='center'>";
								echo"<p align='left'><br><span class=mesg>Database has been exported successfully. <BR><BR><a class=\"Heading-Link\" href=$gblPageAddr"."download.php?fn="."$BackupFileName><u>Click here to download</u></a></span><br>\n<br>\n";
								echo"</td></tr><tr><td align='center'>";
								echo"<input class=\"textbox\" type='button' name='back' value='Back' onClick='javascript:window.history.back();'>";
								echo"</td></tr></table>";
								if(isset($ftp)) {
									echo $ftp;
								}
								echo"<br><br>\n\n";
							}
							}
							}

						}elseif(!isset($HTTP_POST_VARS['db']) && $mode=="web") {
							echo"<form action='exportData.php' method='post'>\n";
							echo"<br><table border='0' width='80%' cellpadding='0' cellspacing = '0' class=admin-text>";
							echo"<tr><td colspan='2' align ='left'>";
							$row->Database ='$db_name';
							echo"<span >Database being exported:</span><span ><B> $db_name</B></span><br>\n";
							echo"<input class=\"textbox\" type='hidden' name='db' value='$db_name'>\n";
							echo"</td></tr>";
							echo"<tr><td colspan='2' align ='left'>&nbsp;</td></tr>";
							echo"<tr><td width='20%' valign='top'>";
							echo"<span >Comments:</span><br>\n";
							echo"</td><td width='80%' valign='top'>";
							echo"<textarea class=\"textbox\" name='comments' rows='3' cols='30'></textarea>\n";
							echo"</td></tr>";
							echo"<tr><td colspan='2' align ='left'>&nbsp;</td></tr>";
							echo"<tr id=2><td colspan='2' valign='top' align='left'>";
							echo"<input class=\"textbox\" type='checkbox' name='tables' checked><span > Export tables</span>";
							echo"</td></tr>";
							echo"<tr><td colspan='2' align ='left'>&nbsp;</td></tr>";
							echo"<tr id=1><td colspan='2' valign='top' align='left'>";
							echo"<input class=\"textbox\" type='checkbox' name='data' checked><span > Export data</span>";
							echo"</td></tr>";
							echo"<tr><td colspan='2' valign='top' align='left'>";
							echo"<br>\n&nbsp;&nbsp;&nbsp;&nbsp;<input class=\"textbox\" type='submit' name='submit' value='Export'>&nbsp;&nbsp;";
							echo"<input class=\"textbox\" type=\"reset\" name='reset' value='Reset'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
							echo"</td></tr></table>";
							echo"</form>";
							echo "<script language=javascript> document.getElementById(\"1\").style.display = \"none\"; document.getElementById(\"2\").style.display = \"none\";</script>";
						}
						@mysql_close($con);
					?>
		</td></tr>
		<tr><td>
	  <input type="hidden" name="fileName" value="">
	  <input type="hidden" name="db_name" value="<?php print "$db_name";?>">
	  <input type="hidden" name="save" value="Save">
	  </td></tr>
	  </form>
	</table></div>
      </div> 
      <div id="corner_container_bottom_for_innerpage"><img src="images/images/innerpage_bottom_corner_820x10.jpg"  height="10" /></div>
    </div> 
      <!-- middle_container ends -->
    
    <div id="bottom_container">
<?php require_once('include/footer.php'); ?>
